class Post {
  final int id;
  final String title;
  final String content;
  final int userId;
  final DateTime postTime;
  final List<String> images;
  final int likes;
  final int comments;
  final String userAvatar;
  final String username;

  Post({
    required this.id,
    required this.title,
    required this.content,
    required this.userId,
    required this.postTime,
    required this.images,
    required this.likes,
    required this.comments,
    required this.userAvatar,
    required this.username,
  });

  // 从JSONPlaceholder的/posts数据转换成Post对象
  factory Post.fromJson(Map<String, dynamic> json) {
    return Post(
      id: json['id'],
      title: json['title'] ?? '宠物分享',
      content: json['body'] ?? '',
      userId: json['userId'] ?? 1,
      postTime: DateTime.now().subtract(Duration(hours: json['id'] ?? 1)),
      images: _generateRandomImages(json['id'] ?? 1),
      likes: (json['id'] ?? 1) * 5 % 300,
      comments: (json['id'] ?? 1) * 2 % 100,
      userAvatar: 'https://via.placeholder.com/150/92c952',
      username: '宠物爱好者${json['userId'] ?? 1}',
    );
  }

  // 生成随机图片链接
  static List<String> _generateRandomImages(int id) {
    List<String> images = [];
    final int imageCount = id % 4 + 1; // 1-4张图片
    
    for (int i = 0; i < imageCount; i++) {
      images.add('https://via.placeholder.com/600/${(id + i)*10}/${(id + i)*20}');
    }
    
    return images;
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'content': content,
      'userId': userId,
      'postTime': postTime.toIso8601String(),
      'images': images,
      'likes': likes,
      'comments': comments,
      'userAvatar': userAvatar,
      'username': username,
    };
  }
} 